<?php
class SF_Translationinterface_Adminhtml_TranslaController extends  Mage_Adminhtml_Controller_Action{

	protected function _isAllowed()
    {
        return true;
    }
	
    public function gettranslasubmiAction(){
        $productIds   = $this->getRequest()->getParam('product');
        $successProductIds = array();
        $productData = array();
        $collection = Mage::getModel('catalog/product')->getCollection()
                ->addAttributeToSelect('*')
                ->addAttributeToFilter('entity_id', array('in'=>$productIds));
                
        foreach($collection as $key => $_product){

            //过滤子产品
            $configurable_ids  = Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($_product->getId());
            if(count($configurable_ids) > 0) continue;

            $approvalStatus = $_product->getApproval();//商品状态
            if($approvalStatus == '5'){
                $productData['sku'] = $_product->getSku();//商品的sku
                $productData['title'] = $_product->getName();//商品名称name
                $productData['longDescription'] = addslashes($_product->getDescription());// 商品长描述description
                $productData['shortDescription'] = addslashes($_product->getShortDescription());//商品的短描述short_description
                $productData['imgUrl'] = (string)Mage::helper('catalog/image')->init($_product, 'image');// 商品图片
                $productData['languageType'] ='en';
                $productDatas[]= $productData;
                $successProductIds[] = $_product->getId();
            }
        }
        if(count($productDatas) > 0){
            $data['json'] = json_encode($productDatas);
            $jsoncurl = $this->sendtranslationinterface($data);
            if(strpos($jsoncurl,'success')==true) {
                $this->commoditystorage($successProductIds);
                $this->_getSession()->addSuccess( 
                    $this->__('Total of %d record(s) have been translation.', count($successProductIds))
                );                     
            }else{
                $this->_getSession()->addError( 
                    $this->__('Submit translation failure')
                );
                Mage::log('Submit translation failure'.$jsoncurl);
            }
        }else{
            $this->_getSession()->addError( 
                $this->__('Only the second instance of the second trial')
            );
            
        }

        $this->_redirectReferer('vendors_catalog_product/pending');
    }


    /*
     * 商品状态保存
     */
    public function commoditystorage($successProductIds){
        $storeId = 0;
        $status  = 6;
        if (empty($successProductIds)) {return false;}

        try {
            Mage::getSingleton('catalog/product_action')
            ->updateAttributes(
                $successProductIds,
                array('approval' => $status),
                $storeId
            );
        }catch (Exception $e) {
        }
        
    }

    /**
     * PHP发送Json对象商品数据
     *
     * @param $url 请求url
     * @param $jsonProduct 发送的json字符串商品信息
     * @return success or error
     */
    function sendtranslationinterface($data)
    {
        $url = Mage::getModel('core/variable')->loadByCode('translateUrl')->getValue('store_html_value');
        $ch = curl_init();
        $timeout = 300;
        curl_setopt($ch, CURLOPT_HEADER,1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $handles = curl_exec($ch);
        curl_close($ch);
        return $handles;
    }
}